Welcome to “Basic Application and Routes.” After watching this video, you will be able
to: Create and run a Flask application with basic
routes in place. Explain how to return JSON response from the
server to clients. Describe various configuration options available
in Flask Before you create your first Flask application,
ensure that you have installed Flask. Next, create a Python file that will be your
server. Let's name this file app.py. Let’s write the code in this file — First,
import the capital F Flask class from the small f Flask module. Next, instantiate an object of the Flask class
as your app. The constructor takes a single argument of class Scaffold. You set the
name in the scaffold by passing the name of the app module in the built-in name variable. This
name is used to find the resources on the filesystem and by extensions to provide debugging
information. Now that you have defined your server, let's
add the first route. You want to return a message to the client when they call your
server without adding a path. You need to use the @app decorator to define
a route. The decorator takes the path as an argument. Finally, you can return text or HTML from
the method. Let’s look at the code. The @app decorator
is defined on the hello_world method. It takes in the argument of "/" and returns the HTML
message "my first Flask application in action!" in bold. The next step is to run your application. The first step is to create a few system environment
variables. You need a variable FLASK_APP containing the
name of the main server file. In addition, you need a FLASK_ENV variable
that will define the development or production environment. This variable will be deprecated
in Flask 2.3. As you can see, you have defined the FLASK_APP
environment variable as the file name that contains the central server and FLASK_ENV
of development. Finally, to run the application, pass the
run argument to the Flask framework by executing ‘Flask run.” The Flask application is run by default on
port 5000. You can browse to http://localhost:5000 to see your message. Let's also use the browser developer tools
to see what information comes back from your server. The requested URL is http://localhost:5000. The request method is HTTP GET. The response has a status of 200, which indicates
a successful response. The content type in the response header is
"text/html." The server is Werkzeug running with a Python
version of 3.6.15 Congratulations on running your first Flask
application. You need to set environment variables for
each application before running your application. You can pass configuration to the flask command
by using "--app" to identify the Python file to run. Add "--debug" to enable the development mode.
The debug flag also enables automatic restarts when source files are changed and is useful
when you develop applications and want to see your changes instantaneously. In your case, the app is stored in a file
called app.py, so you could have skipped this argument, as Flask will look for app.py in
the current directory by default. The output should look something like this.
The screen shows the Flask application running in development mode as before. There are multiple ways to return JSON from
your Flask application. One method is to return a serializable object
like a dictionary or a list. In the given code, you return a Python dictionary.
Flask will use the Python JSON module to return JSON to the client. Let's test if this works using the curl command. You will make a GET request to localhost:5000. You can see a response status of 200 OK. Instead of HTML, you can also see the “application/JSON”
content type. Finally, you can see the JSON that is returned. If you return a more complex object like a
class, ensure that it can be serialized. The second way is to use the jsonify method
provided by Flask. This method takes in key-value pairs at input and returns the appropriate JSON. Let's look at an example. You first import jsonify from Flask. Next, you pass in key-value pairs to jsonify. You should get the same result as before in
the browser. The developer tools should look the same with A status code of 200 OK and return the content type of "application/JSON." You looked at two configurations with FLASK_ENV
and FLASK_APP variables. Flask provides various other configuration options that you might
use in your application: ENV – Indicates the environment, production
or development, the app is running in. DEBUG – enables the debug mode. TESTING – enables the testing mode. SECRET_KEY – used to sign the session
cookie. SESSION_COOKIE_NAME – the name of the session
cookie. SERVER_NAME – binds the host and port, and JSONIFY – defaults to 'application/JSON.' In addition, there are other ways you can
provide configuration options to Flask. Flask provides a config object. You can insert
configuration options into this object. If you already have environment variables,
you can load them into the config object. Finally, you can keep the configuration options
in a separate JSON file and load them using the "from_file" method provided by the config
object. As your app grows, you should create a directory
structure instead of using a single Python file. There are many ways to structure your
application. Here is one example: store the main source code in its module directory, and store all configurations in its file. Store all static assets like image, JavaScript,
and CSS files separately. Store all dynamic content in a template directory. Place all test files in a test directory, and have a virtual environment that can be
activated to install the right version of dependencies. In this video, you learned that: You can create a server by instantiating the
Flask class. Use the @app decorator to create URL handlers. Return string messages or use the jsonify()
method to return JSON objects. You can set application configuration from environment variables, Python files, app. config object directly.